package nowcoder.nc27;

import java.util.*;


public class Solution {
    public static void main(String[] args) {
        System.out.println(new Solution().getLongestPalindrome("bsdfsdfgfds"));
    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * @param A string字符串
     * @return int整型
     */
    public int getLongestPalindrome(String A) {
        int[] res = new int[2];
        char[] chars = A.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            i = findLongest(i, chars, res);
        }
        return res[1] - res[0] + 1;
    }

    private int findLongest(int low, char[] schar, int[] res) {
        int high = low;
        //找到中间位置
        while (high < schar.length - 1 && schar[low] == schar[high + 1]) {
            high++;
        }
        //定位最后的high位置
        int ans = high;
        //判断回文
        while (low > 0 && high < schar.length - 1 && schar[low - 1] == schar[high + 1]) {
            high++;
            low--;
        }
        //更新res位置
        if (high - low > res[1] - res[0]) {
            res[1] = high;
            res[0] = low;
        }
        if (high >= schar.length) return schar.length - 1;
        return ans;
    }
}